package cn.wolfcode.shop.cloud.core;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.concurrent.ConcurrentHashMap;

@ServerEndpoint("/{uuid}")
@Component
@Slf4j
public class WebSocketServer {
    public static ConcurrentHashMap<String, Session> clients = new ConcurrentHashMap<>();

    @OnOpen
    public void onOpen(Session session, @PathParam( "uuid") String uuid){
        log.info("[Socket 服务] 用户:{} 连接上来了...", uuid);
        clients.put(uuid, session);
    }

    @OnClose
    public void onClose(@PathParam( "uuid") String uuid){
        log.info("[Socket 服务] 用户:{} 连接断开了...", uuid);
        clients.remove(uuid);
    }

    @OnError
    public void onError(Throwable error) {
        log.info("[Socket 服务] 连接出现异常...", error);
    }
}